<?php
namespace app\shop\model;

use app\crm\model\FollowRecord;
use app\admin\model\Common;
use Kingdee\Kingdee;
use think\facade\Db;
use think\model\concern\SoftDelete;
use app\common\traits\ActionRecordTrait;
use app\shop\model\RefundPayModel;

class AddOrderLog extends Common
{
    use ActionRecordTrait;

    /**

     * 表名

     * @var string

     */

    protected $name = 'shop_refund_order_log';

    protected $defaultSoftDelete = 0;

    /**

     * 获取sku信息

     * @return \think\model\relation\hasOne

     * @author SL

     * @create 2021-05-27 15:04

     */

    public function orderGoods()

    {

        return $this->hasOne(Goods::class,'id');

    }

    // 新增客户消费记录
    public function addData($customer_id,$shop_id,$order_id,$deal_time,$order_type){

        $addData = [

            'customer_id' => $customer_id,

            'shop_id' => $shop_id,

            'order_no' => $order_id,

            'order_type' => $order_type, // 1退款单 2退货单 3收款单

            'deal_time' => $deal_time,

            'create_time' => time(),

            'is_deleted' => 0,

        ];

        $id = Db::name('shop_refund_order_log')->insertGetId($addData);

        // 修改客户最新消费时间
        $this->updateFinalConsumption($customer_id);

    }

    // 撤销顾客消费记录
    public function deletedData($customer_id,$order_id){

        $where = [];

        array_push($where,['order_no','=',$order_id]);

        Db::name('shop_refund_order_log')->where($where)->update(['is_deleted' => 1]);

        // 修改客户最新消费时间
        $this->updateFinalConsumption($customer_id);
    }

    // 修改客户最新消费时间
    public function updateFinalConsumption($customer_id)
    {

        $where = [];

        array_push($where,['customer_id','=',$customer_id]);

        array_push($where,['is_deleted','=',0]);

        $finalConsumptionTime = Db::name('shop_refund_order_log')->where($where)->field('max(deal_time) as deal_time')->find();

        // echo "修改客户最新消费时间";
        // echo "<pre>";
        // print_r($finalConsumptionTime);
        // echo "</pre>";
        // exit;

        if($finalConsumptionTime){

            $where = [];

            array_push($where,['id','=',$customer_id]);

            Db::name('customer')->where($where)->update(['final_consumption' => $finalConsumptionTime['deal_time']]);

        }

    }

}


?>